using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[DisallowMultipleComponent]
[RequireComponent(typeof(MovementToPositionEvent))]
[RequireComponent(typeof(Rigidbody2D))]
public class MovementToPosition : MonoBehaviour
{
    private Rigidbody2D rigidbody2D;
    private MovementToPositionEvent movementToPositionEvent;
    private void Awake()
    {
        rigidbody2D = GetComponent<Rigidbody2D>();
        movementToPositionEvent = GetComponent<MovementToPositionEvent>();
    }

    private void OnEnable()
    {
        movementToPositionEvent.OnMovementToPosition += MovementToPositionEvent_OnMovementToPosition;
    }

    private void MovementToPositionEvent_OnMovementToPosition(MovementToPositionEvent @event, MovementToPositionEventArgs args)
    {
        MoveRigidBody(args.movePosition, args.currentPosition, args.rollSpeed, args.rollDirection);
    }

    private void MoveRigidBody(Vector3 movePosition, Vector3 currentPosition, float rollSpeed, Vector2 direction)
    {
        /*        Vector2 unitVector = Vector3.Normalize(movePosition - currentPosition);
                rigidbody2D.MovePosition(rigidbody2D.position + (unitVector * rollSpeed * Time.fixedDeltaTime));*/

        rigidbody2D.MovePosition(rigidbody2D.position + (direction * rollSpeed * Time.fixedDeltaTime));
    }

    private void OnDisable()
    {
        movementToPositionEvent.OnMovementToPosition -= MovementToPositionEvent_OnMovementToPosition;
    }
}